//
//  HFFormLocalOptionTVC.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/8/20.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "HFFormLocalOptionTVC.h"

@implementation HFFormLocalOptionTVC
{
    UILabel             *_titleLabel;
    UILabel             *_contentLabel;
//    HFFormOptionsView   *_pickerView;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = UIColorFromRGB(0x555555);
        [self.contentView addSubview:_titleLabel];
        
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_contentLabel];
        
//        _pickerView = [[HFFormOptionsView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] applicationFrame].size.width, iOS8 ? 240 : 260)];
//        _pickerView.pickerView.delegate = self;
//        _pickerView.pickerView.dataSource = self;
//        __weak typeof(self)weakSelf = self;
//        _pickerView.doneBlock = ^{
//            __strong typeof(weakSelf)self = weakSelf;
//            [self resignFirstResponder];
//
//            [self _updateValue];
//        };
//        _pickerView.cancelBlock = ^{
//            __strong typeof(weakSelf)self = weakSelf;
//            [self resignFirstResponder];
//        };
    }
    return self;
}

- (void)updateData:(HFFormRowModel *)row {
    [super updateData:row];
    
    _titleLabel.text = row.title;
    _contentLabel.text = (row.titleHandler && [row.titleHandler() length] > 0) ? row.titleHandler() : (row.content ? [NSString stringWithFormat:@"%@",row.content]: row.placeholder);
    _contentLabel.textColor = row.content ? UIColorFromRGB(0x222222) : UIColorFromRGB(0xe1e1e1);
    
    // 构造数据
    [self _constructData];
    
    // 主要判断回填数据
    NSMutableArray *array = @[].mutableCopy;
    for (NSInteger i = 0; i < row.subRows.count; i++) {
        HFFormRowModel *subRow = row.subRows[i];
        if (subRow.value) {
            BOOL find = NO;
//            for (NSInteger j = 0; j < subRow.multiDatas.count; j++) {
//                HFFormOptionsInterModel *model = subRow.multiDatas[j];
//                if ([model.optionID isEqualToString:[NSString stringWithFormat:@"%@", subRow.value]]) {
//                    find = YES;
//                    [_pickerView.pickerView selectRow:j inComponent:i animated:NO];
//                    [_pickerView.pickerView reloadComponent:i];
//                    [array addObject:model.optionValue];
//                    break;
//                }
//            }
//            if (!find && subRow.defaultSelectRow) {
//                [_pickerView.pickerView selectRow:subRow.defaultSelectRow.integerValue - 1 inComponent:i animated:YES];
//                [_pickerView.pickerView reloadComponent:i];
//            }
            if (!find) {
                subRow.value = nil;
            }
        }else if (subRow.defaultSelectRow) {
            NSInteger j = subRow.defaultSelectRow.integerValue;
//            [_pickerView.pickerView selectRow:j - 1 inComponent:i animated:YES];
//            [_pickerView.pickerView reloadComponent:i];
        }else{
//            [_pickerView.pickerView selectRow:0 inComponent:i animated:YES];
//            [_pickerView.pickerView reloadComponent:i];
        }
    }
    if (array.count > 0) {
        if (row.formatter) {
            NSString *formatter = row.formatter;
            if (array.count == 1) {
                self.row.content = [NSString stringWithFormat:formatter, array[0]];
            }else if(array.count == 2) {
                self.row.content = [NSString stringWithFormat:formatter, array[0], array[1]];
            }else if (array.count == 3) {
                self.row.content = [NSString stringWithFormat:formatter, array[0], array[1], array[2]];
            }else if (array.count == 4) {
                self.row.content = [NSString stringWithFormat:formatter, array[0], array[1], array[2], array[3]];
            }
            
        }else{
            self.row.content = [array componentsJoinedByString:@" "];
        }
        _contentLabel.textColor = UIColorFromRGB(0x222222);
//        switch (self.type) {
//            case HFFormOptionsTypeNormal:{
//                _contentLabel.text = self.row.content;
//            }break;
//
//            case HFFormOptionsTypeFloor:{
//                NSString *content = [array componentsJoinedByString:@"/"];
//                _contentLabel.text = [content stringByReplacingOccurrencesOfString:ASLocalizedString(@"楼")withString:@""];
//            }break;
//
//            default:
//                break;
//        }
    }
}

- (void)_constructData {
    if (self.row.subRows.count > 0) {
        for (long i = 0; i < self.row.subRows.count; i++) {
            HFFormRowModel *row  = self.row.subRows[i];
            NSArray *datas = row.multiDatas;
            row.valueHandler = ^(id value) {
                if (self.row.valueHandler) {
                    self.row.valueHandler(value);
                }
            };
            
            NSMutableArray *multiDatas = @[].mutableCopy;
//            for (long j = 0; j < datas.count; j++) {
//                if ([datas.firstObject isKindOfClass:[NSString class]]) {
//                    HFFormOptionsInterModel *model = [[HFFormOptionsInterModel alloc] init];
//                    model.optionID = datas[j];
//                    model.optionValue = datas[j];
//                    [multiDatas addObject:model];
//                }else if (![datas.firstObject isKindOfClass:[HFFormOptionsInterModel class]]){
//                    NSObject *obj = datas[j];
//                    HFFormOptionsInterModel *model = [[HFFormOptionsInterModel alloc] init];
//                    if (self.row.idKey && self.row.valueKey) {
//                        id value = [obj valueForKey:self.row.idKey];
//                        model.optionID = [NSString stringWithFormat:@"%@",value];
//                        model.optionValue = [obj valueForKey:self.row.valueKey];
//                        [multiDatas addObject:model];
//                    }
//                }
//            }
            if(multiDatas.count > 0) row.multiDatas = multiDatas;
        }
    }
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    _titleLabel.left    = 16;
    _titleLabel.top     = 12;
    _titleLabel.width   = self.width - _titleLabel.left - 50;
    _titleLabel.height  = 24;
    
    _contentLabel.left      = _titleLabel.left;
    _contentLabel.top       = 38;
    _contentLabel.width     = _titleLabel.width;
    _contentLabel.height    = 28;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
